//
//  QCAirPressionCell.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/25.
//

import UIKit

class QCAirPressionCell: UITableViewCell {
    @IBOutlet var title: UILabel!
    @IBOutlet var min: UILabel!
    @IBOutlet var max: UILabel!
    @IBOutlet var slider: UISlider!
    var block:((_ value:Int)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        slider.isContinuous = true
    }
    
    lazy var currentValue : UILabel = {
      let lbl = UILabel()
        lbl.textHexColor = "#FFFFFF"
        lbl.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(lbl)
        return lbl
    }()
    
    var limit : (max:Int,min:Int) = (max:0,min:0){
        didSet{
            max.text = "\(limit.max)"
            min.text = "\(limit.min)"
            slider.minimumValue = Float(limit.min)
            slider.maximumValue = Float(limit.max)
        }
    }
    var value : Int = 0{
        didSet{
            slider.value = Float(value)
            currentValue.text = "\(value)"
            let scale = value <= 0 ? 0.0 : CGFloat(value - limit.min) / CGFloat(limit.max - limit.min)
            currentValue.snp.remakeConstraints{ make in
                make.bottom.equalTo(slider.snp.top).offset(-4)
                make.left.equalTo(slider.snp.left).offset((slider.bounds.width - 11) * scale)
            }
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    @IBAction func changeForSlider(_ sender: UISlider) {
        ///每次变化是5递减/递加
        let s = Int(sender.value - Float(value)) / 1
        value = value + s * 1
        MyPrint("=======value = \(value)")
    }
    
    @IBAction func actionForSlider(_ sender: UISlider) {
        ///每次变化是5递减/递加
        let s = Int(sender.value - Float(value)) / 1
        let n = value + s * 1
        MyPrint("=======block = \(value)")
        block?(n)
    }
    
}
